﻿#include "JZWebWidget.h"
#include "JZWebPage.h"

JZWebWidget::JZWebWidget(JZWebWidgetPage* page)
{
    m_page = page;
    m_id = m_page->generateId();
    m_layout = nullptr;
}

JZWebWidget::~JZWebWidget()
{
}

int JZWebWidget::id()
{
    return m_id;
}

void JZWebWidget::setLayout(JZWebLayout* l)
{
    if (l)
    {
        Q_ASSERT(!l->parent());
        m_layout = l;
        m_layout->setParent(this);
    }
    else
    {
        m_layout = l;
    }
}

JZWebLayout* JZWebWidget::layout()
{
    return m_layout;
}

JZWebContext* JZWebWidget::context()
{
    return m_page->currentContext();
}

void JZWebWidget::setValue(int prop, const QVariant& v)
{
    int key = makeKey(m_id, prop);
    context()->setProperty(key, v);
}

QVariant JZWebWidget::getValue(int prop)
{
    int key = makeKey(m_id,prop);
    return context()->property(key);
}

QDomElement JZWebWidget::render(QDomDocument& doc)
{
    QDomElement ele = doc.createElement("div");
    ele.setAttribute("id", m_id);

    QObjectList list = children();
    for (int i = 0; i < list.size(); i++)
    {
        if (list[i]->inherits("JZWebWidget"))
        {
            auto w = qobject_cast<JZWebWidget*>(list[i]);
            auto sub_ele = w->render(doc);
            sub_ele.setAttribute("id", w->id());
            ele.appendChild(sub_ele);
        }
    }

    if(m_layout)
        ele.appendChild(m_layout->render(doc));
     
    return ele;
}

//JZWebWidgetRoot
JZWebWidgetRoot::JZWebWidgetRoot(JZWebWidgetPage* page)
    :JZWebWidget(page)
{
    setParent(page);
}

JZWebWidgetRoot::~JZWebWidgetRoot()
{

}

QDomElement JZWebWidgetRoot::render(QDomDocument& doc)
{
    QDomElement ele = JZWebWidget::render(doc);
    ele.setTagName("body");
    return ele;
}

//JZWebLabel
JZWebLabel::JZWebLabel(JZWebWidgetPage* parent)
    :JZWebWidget(parent)
{
}

JZWebLabel::~JZWebLabel()
{
}

QDomElement JZWebLabel::render(QDomDocument& doc)
{
    QDomElement ele = doc.createElement("label");
    
    QDomText titleText = doc.createTextNode(text());
    ele.appendChild(titleText);

    return ele;
}

QString JZWebLabel::text()
{
    return getValue(0).toString();
}

void JZWebLabel::setText(QString text)
{
    setValue(0,text);
}

//JZWebLineEdit
JZWebLineEdit::JZWebLineEdit(JZWebWidgetPage* page)
    :JZWebWidget(page)
{
}

JZWebLineEdit::~JZWebLineEdit()
{
}

QDomElement JZWebLineEdit::render(QDomDocument& doc)
{
    QDomElement ele = doc.createElement("input");
    ele.setAttribute("type","text");
    return ele;
}

QString JZWebLineEdit::text()
{
    return QString();
}

void JZWebLineEdit::setText(QString text)
{
}

//JZWebTextBox
JZWebTextBox::JZWebTextBox(JZWebWidgetPage* page)
    :JZWebWidget(page)
{
}

JZWebTextBox::~JZWebTextBox()
{
}

QString JZWebTextBox::text()
{
    return QString();
}

void JZWebTextBox::setText(QString text)
{
}

void JZWebTextBox::append(QString text)
{
}

QDomElement JZWebTextBox::render(QDomDocument& doc)
{
    QDomElement ele = doc.createElement("textarea");
    return ele;
}

//JZWebComboBox
JZWebComboBox::JZWebComboBox(JZWebWidgetPage* page)
    :JZWebWidget(page)
{

}

JZWebComboBox::~JZWebComboBox()
{

}

void JZWebComboBox::addItem(QString text)
{
    m_items.push_back(text);
}

//JZWebCheckBox
JZWebCheckBox::JZWebCheckBox(JZWebWidgetPage* page)
    :JZWebWidget(page)
{

}

JZWebCheckBox::~JZWebCheckBox()
{

}

//JZWebRadioButton
JZWebRadioButton::JZWebRadioButton(JZWebWidgetPage* page)
    :JZWebWidget(page)
{
}

JZWebRadioButton::~JZWebRadioButton()
{
}

//JZWebPushButton
JZWebPushButton::JZWebPushButton(JZWebWidgetPage* page)
    :JZWebWidget(page)
{
}

JZWebPushButton::~JZWebPushButton()
{
}